/*
 * @(#)Timer.java
 *
 */

package diablo3.process;

import diablo3.support.Util;

/**
 * description
 * @author  huangyixiang
 * @version 2012-9-5
 */
public class Timer {
	
//	private static final Log LOG = LogFactory.getLog(Timer.class);
	
	private int hour;
	private int minute;
	private int second;
	
	public Timer(String TimerStr){
//如果逻辑是只要发生任何异常或者错误，所有进程监控就不继续执行，那么就不应该try catch，非受检异常上层不必检查，顶层检查即可，然后继续执行下面的代码
//如果逻辑是只要发生任何异常或者错误，本进程监控就不继续执行，那么就不应该try catch，非受检异常上层检查即可，然后继续执行下面的代码
//如果逻辑是只要发生任何异常或者错误，本进程监控需要继续执行，那么就应该try catch处理掉，然后继续执行下面的代码
//		try{
			String[] times = TimerStr.split("\\:");
			if(times == null || times.length != 3){
				throw new IllegalArgumentException("空的参数");
			}
			for(int i = 0 ; i < 3 ;i++){
				if(Util.isBlank(times[i])){
					throw new IllegalArgumentException("不正确的参数" + i);
				}
				int time = 0;
				try{
					time = Integer.valueOf(times[i]);
				}
				catch (Exception e) {
					throw new IllegalArgumentException("不正确的参数" + times[i]);
				}
				if(i == 0){
					hour = time;
				}
				else if(i == 1){
					minute = time;
				}
				else if(i == 2){
					second = time;
				}
			}
//		}
//		catch (Exception e) {
//			LOG.error("错误的Timer参数", e);
//这里本身就最好不记录日志
//		}
	}
	
	
	public int getHour() {
		return hour;
	}
	public void setHour(int hour) {
		this.hour = hour;
	}
	public int getMinute() {
		return minute;
	}
	public void setMinute(int minute) {
		this.minute = minute;
	}
	public int getSecond() {
		return second;
	}
	public void setSecond(int second) {
		this.second = second;
	}
	
	@Override
	public String toString() {
		String ret = "";
		String hourStr = String.valueOf(hour);
		String minStr = String.valueOf(minute);
		String secondStr = String.valueOf(second);
		if(hour < 10){
			hourStr = "0" + hourStr;
		}
		if(minute < 10){
			minStr = "0" + minStr;
		}
		if(second < 10){
			secondStr = "0" + secondStr;
		}
		ret = hourStr + ":" + minStr + ":" + secondStr;
		return ret;
	}
	
	public int compare(Timer timer){
		int total1 = this.hour * 3600 + this.minute * 60 + this.second;
		int total2 = timer.hour * 3600 + timer.minute * 60 + timer.second;
		if(total1 > total2){
			return 1;
		}
		else if(total1 < total2){
			return -1;
		}
		else {
			return 0;
		}
	}
	
public static void main(String[] args) {
	System.out.println(new Timer("220:110:111"));
}
}
